package com.park.controller;

import com.park.DingdongCloudApis;
import com.park.common.annotation.Protected;
import com.park.common.bean.ResponseBean;
import com.park.common.constant.IPlatformConstant;
import com.park.common.exception.MessageException;
import com.park.common.poo.ExpressInfo;
import com.park.common.poo.UserCollector;
import com.park.common.poo.UserOperator;
import com.park.common.poo.UserSign;
import com.park.common.util.DateUtil;
import com.park.common.util.JsonUtils;
import com.park.common.util.StrUtil;
import com.park.platform.service.IExpressService;
import com.park.platform.service.IOperatorService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;
import java.util.Random;

/**
 * Created by leida on 2017/5/3.
 */
@Controller
@RequestMapping(value="/user")
public class OperatorController extends BaseController {

    private Logger logger = Logger.getLogger(OperatorController.class);

    @Autowired
    private IOperatorService operatorService;

    @Autowired
    private IExpressService expressService;

    @ResponseBody
    @RequestMapping("loginMobile")
    public ResponseBean login(String mobile, String password, HttpServletResponse response){
        try {
            UserOperator userOperator = operatorService.login(mobile, password);
            super.setUserOperatorCookie(mobile, password, response);
            super.getRequest().getSession().setAttribute(IPlatformConstant.LOGIN_USER, userOperator);
            return new ResponseBean(true);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @ResponseBody
    @RequestMapping("registerMobile")
    public ResponseBean newUserOperator(UserOperator userOperator, String captcha, HttpServletResponse response){
        try {
            String captchaCase = StrUtil.objToStr(super.getRequest().getSession().getAttribute(IPlatformConstant.REG_MOBILE_CAPTCHA));
            if(StrUtil.isBlank(captcha)) throw new MessageException("请输入验证码:)");
            if(!captcha.equals(captchaCase)) throw new MessageException("验证码不正确:(");
            operatorService.addUserOperator(userOperator);
            super.setUserOperatorCookie(userOperator.getOperatorMobile(), userOperator.getOperatorPwd(), response);
            super.getRequest().getSession().setAttribute(IPlatformConstant.LOGIN_USER, userOperator);
            return new ResponseBean(true);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @ResponseBody
    @RequestMapping("registerCaptcha")
    public ResponseBean registerCaptcha(UserOperator userOperator){
        try {
            String mobile = userOperator.getOperatorMobile();
            operatorService.registerCaptcha(userOperator);
            int code = 1000 + new Random().nextInt(8999);
            logger.info(mobile + "下发短信验证码：" + code);
            String msgJson = DingdongCloudApis.sendYzm(mobile, code);
            logger.info("======================="+msgJson+"=======================");
            super.getRequest().getSession().setAttribute(IPlatformConstant.REG_MOBILE_CAPTCHA, code);
            boolean yzm = StrUtil.objToInt(JsonUtils.fromJsonDF(msgJson, Map.class).get("code")) == 1;
            if(yzm) return new ResponseBean(true);
            else{
                ResponseBean responseBean = new ResponseBean(false);
                responseBean.setMessage("验证码发送失败，请重试");
                return responseBean;
            }
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @Protected
    @ResponseBody
    @RequestMapping("loginOut")
    public ResponseBean loginOut(HttpServletResponse response){
        try {
            System.out.println("=================5====================");
            super.getRequest().getSession().invalidate();
            super.clearUserOperatorCookie(response);
            return new ResponseBean(true);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @Protected
    @RequestMapping("usercenter")
    public String usercenter(Model model){
        try {
            UserOperator userOperator = operatorService.getUserOperator(getUserInfo2().getId());
            model.addAttribute(userOperator);

            String nowSign = DateUtil.dateToString(new Date(), DateUtil.YYYYMMDD);
            model.addAttribute("nowSign", !nowSign.equals(userOperator.getLastSignDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        return "member";
    }

    @Protected
    @ResponseBody
    @RequestMapping("sign")
    public ResponseBean sign(UserSign userSign){
        try {
            userSign.setId(getUserInfo2().getId());
            operatorService.updateUserSign(userSign);
            return new ResponseBean(true);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @Protected
    @ResponseBody
    @RequestMapping("newExpress")
    public ResponseBean newExpress(ExpressInfo expressInfo){
        try {
            expressInfo.setId(getUserInfo2().getId());
            expressService.newExpress(expressInfo);
            return new ResponseBean(true);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @Protected
    @ResponseBody
    @RequestMapping("newCollector")
    public ResponseBean newCollector(UserCollector userCollector){
        try {
            UserOperator userInfo2 = getUserInfo2();
            if(userInfo2 == null) throw new MessageException("请您先登录，再收藏");
            userCollector.setId(userInfo2.getId());
            operatorService.newCollector(userCollector);
            return new ResponseBean(true);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

}
